package com.cn.chaomeng.service.impl;

import com.cn.chaomeng.dao.FoodMapper;
import com.cn.chaomeng.dao.FoodtypeMapper;
import com.cn.chaomeng.dao.OrderfoMapper;
import com.cn.chaomeng.model.Food;
import com.cn.chaomeng.model.Foodtype;
import com.cn.chaomeng.model.Orderfo;
import com.cn.chaomeng.model.example.FoodExample;
import com.cn.chaomeng.model.example.FoodtypeExample;
import com.cn.chaomeng.model.example.OrderfoExample;
import com.cn.chaomeng.model.example.OrdersExample;
import com.cn.chaomeng.service.IFoodsService;
import com.cn.chaomeng.utils.DateUtil;
import com.cn.chaomeng.vo.BootstrapTableVo;
import com.cn.chaomeng.vo.table.FoodSalesTable;
import com.cn.chaomeng.vo.table.FoodTypeTable;
import com.cn.chaomeng.vo.table.FoodsTable;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author LYJ
 *         Created by Administrator on 2018/2/28 0028.
 */
@Service("foodService")
public class FoodServiceImpl implements IFoodsService{

    @Resource
    private FoodMapper foodMapper;

    @Resource
    private FoodtypeMapper foodtypeMapper;

    @Override
    public List<Foodtype> selectFoodType() {
        return foodtypeMapper.selectByExample(new FoodtypeExample());
    }

    @Override
    public Foodtype selectFoodTypeById(Integer id) {
        return foodtypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public Foodtype selectFoodTypeByName(String name) {
        FoodtypeExample example = new FoodtypeExample();
        example.createCriteria().andTypenameEqualTo(name);
        List<Foodtype> foodtypes = foodtypeMapper.selectByExample(example);
        return foodtypes != null && foodtypes.size() != 0 ? foodtypes.get(0) : null;
    }

    @Override
    public boolean insertFoodType(Foodtype foodtype) {
        return foodtypeMapper.insertSelective(foodtype) != 0;
    }

    @Override
    public boolean updateFoodTypeById(Foodtype foodtype) {
        return foodtypeMapper.updateByPrimaryKeySelective(foodtype) != 0;
    }

    @Override
    public boolean deleteFoodTypeById(Integer id) {
        return foodtypeMapper.deleteByPrimaryKey(id) != 0;
    }

    @Override
    public int selectCount() {
        return (int)foodMapper.countByExample(new FoodExample());
    }

    @Override
    public Food selectById(Integer id) {
        return foodMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Food> selectByIds(List<Integer> ids) {
        FoodExample example = new FoodExample();
        example.createCriteria().andFidIn(ids);
        return foodMapper.selectByExample(example);
    }

    @Override
    public int selectCountByTypeid(Integer typeId) {
        FoodExample example = new FoodExample();
        example.createCriteria().andTypeidEqualTo(typeId);
        return (int)foodMapper.countByExample(example);
    }

    @Override
    public List<Food> selectPage(Integer offset, Integer limit) {
        return foodMapper.selectByExampleWithRowbounds( new FoodExample(),new RowBounds(offset, limit));
    }

    @Override
    public boolean insert(Food food) {
        return foodMapper.insertSelective(food) != 0;
    }

    @Override
    public boolean updateById(Food food){
        return foodMapper.updateByPrimaryKeySelective(food) != 0;
    }

    @Override
    public boolean updateTypeIdByTypeId(Integer typeId, Integer targetTypeId) {
        FoodExample example = new FoodExample();
        example.createCriteria().andTypeidEqualTo(typeId);
        Food food = new Food();
        food.setTypeid(targetTypeId);
        return foodMapper.updateByExampleSelective(food,example) != 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        return foodMapper.deleteByPrimaryKey(id) != 0;
    }

    @Override
    public BootstrapTableVo getFoodsTable(String where,Integer foodTypeId,Integer offset, Integer limit) {
        /** 获取食物类型 */
        Map<Integer,Foodtype> foodtypeMap = new HashMap<Integer, Foodtype>(16);
        for (Foodtype foodtype : selectFoodType()){
            foodtypeMap.put(foodtype.getTypeid(),foodtype);
        }

        /** 获取食品 */
        RowBounds rowBounds = new RowBounds(offset,limit);
        FoodExample example = new FoodExample();
        FoodExample.Criteria criteria =  example.createCriteria();
        example.setOrderByClause("state desc");
        if(where != null && !"".equals(where)){
            criteria.andFoodnameLike(where);
        }
        if(foodTypeId!= null && foodTypeId != 0){
            criteria.andTypeidEqualTo(foodTypeId);
        }
        List<Food> foods = foodMapper.selectByExampleWithRowbounds(example,rowBounds);

        /** 转为页面表需要的vo */
        BootstrapTableVo bootstrapTableVo = new BootstrapTableVo();
        List<FoodsTable> foodsTable = new ArrayList<FoodsTable>();
        for (Food food : foods){
            foodsTable.add(new FoodsTable(food,foodtypeMap.get(food.getTypeid())));
        }
        bootstrapTableVo.setTotal((int)foodMapper.countByExample(example));
        bootstrapTableVo.setRows(foodsTable);
        return bootstrapTableVo;
    }

    @Override
    public BootstrapTableVo getFoodTypeTable() {
        BootstrapTableVo bootstrapTableVo = new BootstrapTableVo();
        List<FoodTypeTable> foodTypeTables = new ArrayList<FoodTypeTable>();
        FoodExample example = new FoodExample();
        example.createCriteria().andStateNotEqualTo(0);
        for(Foodtype foodtype : foodtypeMapper.selectByExample(new FoodtypeExample())){
            example = new FoodExample();
            example.createCriteria().andTypeidEqualTo(foodtype.getTypeid());
            foodTypeTables.add(FoodTypeTable.create(foodtype,(int)foodMapper.countByExample(example)));
        }
        bootstrapTableVo.setTotal((int)foodtypeMapper.countByExample(new FoodtypeExample()));
        bootstrapTableVo.setRows(foodTypeTables);
        return bootstrapTableVo;
    }



    @Override
    public BootstrapTableVo getFoodSalesTable() {
//        BootstrapTableVo vo = new BootstrapTableVo();
//
//        OrderfoExample example = new OrderfoExample();
//        Date month =  DateUtil.getFirstMonth(1);
//        Date start = DateUtil.getFirstDayOfMonth(month);
//        Date end = DateUtil.getLastDayOfMonth(month);
//        example.createCriteria().andCreateTimeBetween(DateUtil.getDateFormat(start),DateUtil.getDateFormat(end));
//        List<Orderfo> orderfos = orderfoMapper.selectByExample(example);
//
//
//
//        FoodSalesTable table = new FoodSalesTable();
//        return ;
        return null;
    }
}
